// pages/medicalEquipment/medicalEquipment.js
var app = getApp();
Page({
  data: {
    currentTab: 'rent', // 当前tab：rent-租赁 sale-售卖
    searchKeyword: '', // 搜索关键词
    equipmentList: [], // 器械列表
    page: 1, // 当前页码
    pageSize: 10, // 每页条数
    hasMore: true, // 是否还有更多数据
    loading: false, // 是否正在加载
  },

  onLoad() {
    this.loadEquipmentList();
  },

  // 切换Tab
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    if (this.data.currentTab === tab) return;
	console.log('tab:',tab)
    
    this.setData({
      currentTab: tab,
      equipmentList: [],
      page: 1,
      hasMore: true
    });
	this.loadEquipmentList();
  },

  // 搜索输入
  onSearchInput(e) {
    const keyword = e.detail.value.trim();
    this.setData({
      searchKeyword: keyword,
      equipmentList: [],
      page: 1,
      hasMore: true
    });
	this.loadEquipmentList();
  },

  // 加载器械列表
  loadEquipmentList(isLoadMore = false) {
    if (this.data.loading || !this.data.hasMore) return;
    this.setData({ loading: true });
    var that = this
    const { page, pageSize, searchKeyword, currentTab } = this.data;
	console.log('page:',page)
      app.util.request({
              url: 'Qxzl/listsindex',
              data: {
                keyword: searchKeyword,
                saletype: currentTab,
    			page: !isLoadMore ? 1 : page+1,
    			pageSize,
              },
              complete(res) {
				  that.setData({
						hasMore: res.data.data.list.length >= pageSize
				  });
              },
              success(res) {
				  var data = res.data.data;
				  console.log(data)
				  var list = data.list
				  console.log(list)
				  let nowequip = that.data.equipmentList || [];
				  console.log(nowequip)
				  if(list.length > 0){
					  for (let i = 0; i < list.length; i++) {
					      nowequip.push(list[i]);
					  } 
				  }
				  console.log(list.length)
				  console.log(pageSize)
				  console.log(list.length >= pageSize)
				  that.setData({
						equipmentList: nowequip,
						page: isLoadMore ? page + 1 : 1,
						loading: false,
						hasMore: list.length >= pageSize
				  });
              }
      })
  },


  // 加载更多
  loadMore() {
    this.loadEquipmentList(true);
  },


  // 查看器械详情
  viewEquipmentDetail(e) {
    const newsId = e.currentTarget.dataset.id;
	const tab = this.data.currentTab
    wx.navigateTo({
      url: `/vp_pz/pages/sbzl/detail?id=${newsId}&type=${tab}`
    });
  }
});